package com.example.testone

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import cn.iwgang.countdownview.CountdownView
import com.example.testone.databinding.FragmentSettingBinding
import com.example.testone.databinding.FragmentTipBinding
import com.example.testone.utils.MyLog
import com.example.testone.viewModels.SettingViewModel
import java.util.*
import kotlin.concurrent.timerTask

class SettingFragment : Fragment() {
    val TAG = "SettingFragment"
    lateinit var binding: FragmentSettingBinding

    val viewModel: SettingViewModel by activityViewModels()

    //倒计时
    lateinit var settingCountdown: CountdownView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        //设置默认值
        MyLog.print(TAG,"-----------------------------------------------------")
//        Timer().schedule(timerTask {
//            viewModel.settingRunningState.value = 1
//        },1000)
        settingDefaultValue()


        binding = FragmentSettingBinding.inflate(inflater,container,false)
        binding.viewModel = viewModel
        binding.lifecycleOwner = this

        settingCountdown = binding.countdownView

        initBinding()
        //开启设备
        Timer().schedule(object: TimerTask(){
            override fun run() {
                viewModel.settingRunningState.postValue(1)
            }
        },900)
        // Inflate the layout for this fragment
        return binding.root
        // Inflate the layout for this fragment
    }

    private fun initBinding() {

        //倒计时完成
        settingCountdown.setOnCountdownEndListener {
            //触发停止设备状态
            MyLog.print(TAG,"settingRunningState.settingCountdown.setOnCountdownEndListener:${it}")
            viewModel.settingRunningState.value = 0
//            //重置默认温度
            settingDefaultValue()
            //直接返回首页
            findNavController().navigate(R.id.action_settingFragment_to_mainFragment)
        }




//        运行状态
        viewModel.settingRunningState.observe(viewLifecycleOwner){
            if(it<0)return@observe

            MyLog.print(TAG, "-----settingRunningState---${it}")
//            sendData(viewModel.currentBtn,"settingRunningState", it)
            when(it){
                1 -> { //开始
                    if(settingCountdown.second >0 ){
                        settingCountdown.restart()
                    }else{
                        viewModel.countdownTime.value = viewModel.timeSetting.value
                    }

                }
                2 -> { //暂停
                    settingCountdown.pause()
                }
                0 -> { //停止
                    //初始化配置
//                    initSetting()
                    //提示是否返回首页
//                    findNavController().navigate(R.id.action_settingFragment_to_mainFragment)
                }
            }
        }


        //设置时间
        viewModel.timeSetting.observe(viewLifecycleOwner){
            MyLog.print(TAG,"timeSetting:${it}")
            viewModel.countdownTime.value = it
        }

        //倒计时
        viewModel.countdownTime.observe(viewLifecycleOwner){
            MyLog.print(TAG,"countdownTime:${it}-isWorking${viewModel.isWorking}")
            if(it<=0)return@observe
            //延长30秒
            settingCountdown.start(it!!  *60* 1000L)
//            if ( !viewModel.isWorking ) {
//                settingCountdown.stop()
//            }
        }
    }

    fun initSetting()
    {
        //停止倒计时
        settingCountdown.updateShow(0L) //重置时间
        settingCountdown.stop()

        settingDefaultValue()
    }

    fun settingDefaultValue()
    {

        //开启设备
        Timer().schedule(object: TimerTask(){
            override fun run() {
                viewModel.timeSetting.postValue(SettingViewModel.TIME_SETTING)
                viewModel.countdownTime.postValue(SettingViewModel.TIME_SETTING)
                viewModel.wdSetting.postValue(SettingViewModel.WD_SETTING)
                viewModel.voiceNumber.postValue(SettingViewModel.VOICE_NUMBER)
                viewModel.lightNumber.postValue(SettingViewModel.LIGHT_NUMBER)
                viewModel.wdNumber.postValue(SettingViewModel.WD_NUMBER)



            }
        },200)

        //恢复默认时间
//        viewModel.timeSetting.value = SettingViewModel.TIME_SETTING
//        viewModel.countdownTime.value = SettingViewModel.TIME_SETTING
//        viewModel.wdSetting.value = SettingViewModel.WD_SETTING
//        viewModel.voiceNumber.value = SettingViewModel.VOICE_NUMBER
//        viewModel.lightNumber.value = SettingViewModel.LIGHT_NUMBER
//        viewModel.wdNumber.value = SettingViewModel.WD_NUMBER
    }

}